﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using AEA.RSSFeedReaderLib;

namespace AEA.RSSFeedReader
{
    /// <summary>
    /// Interaction logic for AddFeedUserControl.xaml.
    /// </summary>
    [TemplatePart(Name = AddFeedUc.StoryBoardFeedValidationFailed, Type = typeof(Storyboard))]
    public partial class AddFeedUc : Window
    {
        #region Data
        private const string StoryBoardFeedValidationFailed = "STORYBOARD_FeedValidationFailed";
        private Storyboard _sbFeedValidationFailed;
        #endregion

        #region Constructor
        /// <summary>
        /// Initialize a new instance of the AddFeedUc class.
        /// </summary>
        public AddFeedUc()
        {
            this.InitializeComponent();
            _sbFeedValidationFailed = (this.Content as FrameworkElement).TryFindResource
                (AddFeedUc.StoryBoardFeedValidationFailed) as Storyboard;

            Commands.AutoAddFeedUriCommand.Execute(null, this);
        }

        #endregion

        #region Public Properties
        /// <summary>
        /// Gets the inputted text.
        /// </summary>
        public string InputText
        {
            get
            {
                return this._inputText.Text;
            }
            private set
            {
                this._inputText.Text = value;
            }
        }
        #endregion

        #region Event Handlers
        /// <summary>
        /// Event handler for when the user control is activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnActivated(object sender, EventArgs e)
        {
            _inputText.Focus();
        }

        /// <summary>
        /// Event handler for when the cancel button is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClicked(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
        #endregion

        #region Commands
        /// <summary>
        /// The command to add a new feed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoAddFeedCommand(object sender, ExecutedRoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        /// <summary>
        /// Determines if the DoAddFeedCommand can be executed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CanExecuteAddFeedCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = !string.IsNullOrEmpty(_inputText.Text)
                && RSSFeed.IsValidUri(_inputText.Text);
        }

        /// <summary>
        /// The command to paste a Uri from the clipboard automatically.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoAutoAddFeedUroCommand(object sender, ExecutedRoutedEventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                string rawText = Clipboard.GetText().Trim();

                if (RSSFeed.IsValidUri(rawText))
                    _inputText.Text = rawText;
            }
        }

        /// <summary>
        /// The command to close this dialog.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoExitCommand(object sender, ExecutedRoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
        #endregion
    }
}